package com.clqteam.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.clqteam.model.Employee;
import com.clqteam.service.EmployeeLoginService;
import com.clqteam.service.OrderService;
import com.clqteam.serviceimpl.EmployeeLoginServiceImpl;
import com.clqteam.serviceimpl.OrderServiceImpl;

/**
 * 删除订单控制层
 * @author ZHX
 *
 */
public class DelOrderController extends HttpServlet{

	OrderService oSer = new OrderServiceImpl();
	EmployeeLoginService elgSer = new EmployeeLoginServiceImpl();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("UTF8");
		resp.setCharacterEncoding("UTF8");
		
		HttpSession session = req.getSession();
		Employee e = (Employee)session.getAttribute("employee");
		int eId = e.geteId();
		String eName=e.geteName();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String drTime = sdf.format(date);		
		
		int flags = 0;
		String[] ids = req.getParameterValues("ids");
		for(int i=0;i<ids.length;i++){
			int oId = Integer.parseInt(ids[i]);
			int flag = oSer.deleteOrderByOId(oId);
			String drInfo = "删除订单"+oId;
			elgSer.insertLoginTime(drTime, drInfo, eId, eName);
			if(flag==1){
				flags++;
			}
		}
		PrintWriter pw = resp.getWriter();		
		if(flags==ids.length){
			pw.write("1");
		}else{
			pw.write("0");
		}
		pw.flush();
		pw.close();
	}	
}
